package rpg.core
{
	import rpg.Utils.Log;
	import rpg.data.Spell;
	import rpg.role.Hero;
	import rpg.uiView.Ico;

	public class CDComponent extends FrameComponent
	{
		private var role : Hero;
		private var shortCutIocs : Array;

		public function CDComponent(type : * = null)
		{
			super(type ? type : CDComponent);
		}

		override public function notifyAdded() : void
		{
			role = owner as Hero;
			shortCutIocs = role.shortCutIocs;
		}

		override public function update(delay : uint) : void
		{
			for each(var ico : Ico in shortCutIocs)
			{
				if(!ico.isCD || !ico.data)
					continue;
				ico.updateCoolAnimtion(delay);
			}
		}

		public function createCD(spell : Spell) : void
		{
			if(!spell)
			{
				Log.warning("不可创建空技能的冷却时间");
				return;
			}
			createGlobalCD(spell.globalCoolTime);
			var ico : Ico = getIco(spell.id);
			if(ico)
			{
				ico.createCD(spell.selfCoolTime + spell.globalCoolTime);
			}
		}

		public function isCD(id : int) : Boolean
		{
			var ico : Ico = getIco(id);
			if(!ico)
				return true;
			return ico.isCD;
		}

		private function getIco(id : int) : Ico
		{
			for each(var ico : Ico in shortCutIocs)
			{
				if(ico.data.id == id)
				{
					return ico;
				}
			}
			return null;
		}

		/**
		 * 创建公共CD
		 * @param time       cd时间
		 * @param mustUpdate 是否强制更新
		 *
		 */
		private function createGlobalCD(time : int , mustUpdate : Boolean = false) : void
		{
			for each(var ico : Ico in shortCutIocs)
			{
				if(!ico.isCD || mustUpdate)
					ico.createCD(time);
			}
		}
	}
}